package com.sgb.kjwl.view.ui.activity.self.lease;

import android.content.Context;
import android.content.Intent;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivityInvoiceAddBinding;
import com.sgb.kjwl.databinding.ActivityLeaseInvoiceAddBinding;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.NoSymbolputFilter;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

/**
 * 租赁订单-新增发票
 */
public class LeaseInvoiceAddActivity extends AppActivity {

    private ActivityLeaseInvoiceAddBinding binding;

    public static void start(Context context) {
        Intent intent = new Intent(context, LeaseInvoiceAddActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(LeaseInvoiceAddActivity.this, R.layout.activity_lease_invoice_add);
        binding.topLayout.titleView.setText("新增发票信息");
        binding.topLayout.ivBack.setOnClickListener(v -> onBackPressed());
        binding.itemPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.itemBankNo.setInputType(InputType.TYPE_CLASS_NUMBER);
//        binding.itvCreditCode.setNumberAndLetter("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        binding.tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                checkInput();
            }
        });
        binding.tvCancel.setOnClickListener(v -> onBackPressed());
    }

    @Override
    protected void init() {
    }

    private void checkInput() {
        if (TextUtils.isEmpty(binding.itvCName.getContent().trim())) {
            MToast.showToast(LeaseInvoiceAddActivity.this, "请填写单位名称");
            return;
        } else if (TextUtils.isEmpty(binding.itvCreditCode.getContent().trim())) {
            MToast.showToast(LeaseInvoiceAddActivity.this, "请填写税号");
            return;
        } else if (!TextUtils.isEmpty(binding.itvCreditCode.getContent().trim())) {
            if (!binding.itvCreditCode.getContent().trim().matches("^[A-Za-z0-9]+$")) {
                MToast.showToast(LeaseInvoiceAddActivity.this, "税号只能输入数字和英文");
                return;
            }
        }
        if (!TextUtils.isEmpty(binding.itemPhone.getContent().trim())) {
            if (binding.itemPhone.getContent().trim().length() < 5) {
                MToast.showToast(LeaseInvoiceAddActivity.this, "请填写5-20个数字以内的电话号码");
                return;
            }
        }
//        } else if (TextUtils.isEmpty(binding.itemBank.getContent().trim())) {
//            MToast.showToast(LeaseInvoiceAddActivity.this, "请填写开户银行");
//            return;
//        } else if (binding.itemBank.getContent().trim().length() < 4) {
//            MToast.showToast(LeaseInvoiceAddActivity.this, "请填写正确开户银行");
//            return;
//        } else if (TextUtils.isEmpty(binding.itemBankNo.getContent().trim())) {
//            MToast.showToast(LeaseInvoiceAddActivity.this, "请填写银行账号");
//            return;
        if (!TextUtils.isEmpty(binding.itemBankNo.getContent().trim())) {
            if (binding.itemBankNo.getContent().trim().length() < 6) {
                MToast.showToast(LeaseInvoiceAddActivity.this, "请填写6-30个数字以内的银行账号");
                return;
            }
        }
        addInvoice();
    }


    @Override
    public void onBackPressed() {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("提示")
                .setMsg("确定要退出吗？已填写的内容将不会保存！")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        LeaseInvoiceAddActivity.this.finish();
                    }

                    @Override
                    public void onLeftClick() {
                        return;
                    }
                }).show();
    }

    //保存新增发票
    public void addInvoice() {
        Map map = new HashMap<>();
        map.put("address", binding.itvAddress.getContent().trim());//地址
        map.put("bank", binding.itemBank.getContent().trim());//开户银行
        map.put("bankAccount", binding.itemBankNo.getContent().trim());//银行账号
        map.put("companyName", binding.itvCName.getContent().trim());//单位名称
        map.put("number", binding.itvCreditCode.getContent().trim());//税号
        map.put("tel", binding.itemPhone.getContent().trim());//企业电话
        //map.put("id", "");//发票编号，新增不填
        APIManager.getInstance().getLeaseAPI().personalInvoiceInfo(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                MToast.showToast(LeaseInvoiceAddActivity.this, "保存成功");
                finish();
            }
        });
    }
}
